import React from 'react';
import { observer, inject } from 'mobx-react';
import { routerShape } from 'react-router';
import { WeaTools,WeaErrorPage,WeaRightMenu,WeaTop,WeaLocaleProvider } from 'ecCom';
import { Button } from 'antd';
import Condition from './Condition';
import * as _ from 'lodash';
import '../../style/batchPrint.less';
const getLabel = WeaLocaleProvider.getLabel;

@inject("wfBatchPrintIndexStore")
@observer
class PrintIndex extends React.Component{
    static contextTypes = {
        router: routerShape
    }
    componentDidMount(){
        this.doInit(this.props);
        if(window.location.pathname.indexOf('/spa/workflow/index') > -1)
            document.title = this.props.wfBatchPrintIndexStore.title;
    }
    componentWillReceiveProps(nextProps) {
        if(this.props.location.key !== nextProps.location.key)
        	this.doInit(nextProps);
    }
    doInit(props){
        const { wfBatchPrintIndexStore } = props;
        const queryParams = props.location.query || {};
        wfBatchPrintIndexStore.setUrlParams(queryParams);
        wfBatchPrintIndexStore.loadFixedCondition();
        wfBatchPrintIndexStore.setWorkflowid();
    }
    resetHeight = (height) => {
        jQuery(".wf-print-condition").height(height - 60);
    }
    render(){
        const { wfBatchPrintIndexStore } = this.props;
        const { loading,title } = wfBatchPrintIndexStore;
        return (
            <div className="wf-print-index">
                <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@coe6db`} datas={this.getRightMenu()}>
                    <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@cstn0g`}
                        title={title}
                        loading={loading}
                        icon={<i className='icon-coms-workflow' />}
                        iconBgcolor='#0079DE'
                        getHeight={this.resetHeight}
                        buttons={[]}
                        showDropIcon={true}
                        dropMenuDatas={this.getRightMenu()} 
                    >
                        <div className="wf-print-condition">
                            <Condition ecId={`${this && this.props && this.props.ecId || ''}_Condition@32o5ze`} store={wfBatchPrintIndexStore} onEnterSearch={this.forwardPrintResult} />
                        </div>
                        <div className="wf-print-btns">
                            {this.getSearchButtons()}
                        </div>
                    </WeaTop>
                </WeaRightMenu>
            </div>
        )
    }
    getRightMenu(){
    	let btns = [];
    	btns.push({
            key: "search",
    		icon: <i className='icon-workflow-Right-menu--search'/>,
            content:getLabel(197,'搜索'),
            onClick: this.forwardPrintResult
    	});
    	return btns;
    }
    getSearchButtons() {
        const { wfBatchPrintIndexStore } = this.props;
        const btnStyle = {borderRadius: 3, height: 28, width: 80}
        return [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@m8pz2e@search`} type="primary" style={btnStyle} onClick={this.forwardPrintResult}>{getLabel(197,'搜索')}</Button>),
            (<span style={{width:'15px', display:'inline-block'}}></span>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@76q7q5@reset`} type="ghost" style={btnStyle} onClick={this.reset}>{getLabel(2022,'重置')}</Button>)
        ]
    }
    reset = ()=>{this.props.wfBatchPrintIndexStore.resetCondition()};
    forwardPrintResult = () => {
        const { wfBatchPrintIndexStore } = this.props;
        const { urlParams } = wfBatchPrintIndexStore;
        //路由跳转，跳转后做store拷贝条件
        this.context.router.push({
            pathname: "/main/workflow/batchPrintResult",
            query: {...{fromwhere:"index"}, ...urlParams}
        });
    }
}

export default WeaTools.tryCatch(React, 
    props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@hkvxjy`} msg={ props.error ? props.error : getLabel(383324,' 对不起，该页面异常，请联系管理员 ')+'！'} />,
    {error: ""}
)(PrintIndex);